'use strict';

const Controller = require('egg').Controller;
class ArticleListController extends Controller {
    // 获取文章列表

    async searchLists() {
        const { ctx } = this;
        const { pageCode, pageSize, name, num } = ctx.request.body;
        // console.log(user,'11')
        // console.log(status, 'kkkka')

        let lists = await this.app.mysql.select('searchLists');
        // console.log(lists.length)
        let newLists = lists;
        // console.log(num,'ii')
        if (name) {
            newLists = newLists.filter(v => v.name.includes(name))
        }
        if (num) {
            newLists = newLists.filter(v => v.num == Number(num))
        }
        console.log(num,'kk')
        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        // console.log(newLists, 'ff')

        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除文章列表
    async deleteSearchLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('searchLists', { id });
        if (result) {
            const list = await this.app.mysql.select('searchLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }
   

   

    // 多选删除
    async moreDel() {
        const { ctx } = this;
        const { multipleSelection, pageCode, pageSize } = ctx.request.body;
        console.log(multipleSelection,'lll')
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.delete('searchLists', { id: v.id })
        });
        console.log(chooseLists,'jjj')
        if (chooseLists) {
            const list = await this.app.mysql.select('searchLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    list: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
                    total: list.length
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '删除失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '多选删除错误',
            }
        }
    }

    //批量删除
    // async moreDel() {
    //     const { ctx } = this;
    //     const { id } = ctx.request.body
    //     console.log(id,'====')
    //     let sql = `DELETE FROM searchLists WHERE id in (${id})`

    //     try {
    //         const results = await this.app.mysql.query(sql)
    //         ctx.body = {
    //             code: 200,
    //             msg: '删除成功',
    //         }
    //     } catch (err) {
    //         ctx.body = {
    //             code: 505,
    //             msg: '服务器繁忙，请稍后重试',
    //             err,
    //         }
    //     }
    // }
  
   

}

module.exports = ArticleListController;
